import java.util.Arrays;
class Student{
    @Override
    public String toString() {
        return "Student{}";
    }
}
public class Test {
    public static void main(String[] args) {
        String s1 = "109.28.101.156";
        String[] S2 = s1.split("\\.");//如果以.,+,-,*,|为分隔，必须通过\\转义
        for (int i = 0; i < S2.length; i++) {
            System.out.print(S2[i]+" " );
        }
        String s2 = "109\\28\\101\\156";//109\28\101\156
        System.out.println(s2);
     String[] S = s2.split("\\\\");//\\\\
        for (int i = 0; i < S.length; i++) {
            System.out.print(S[i]+" " );
        }
    }
    public static void main10(String[] args) {
        //分割后的结果存储到数组中
        String s1 = "name = zhangsan| age = 10";
       String[] S2 = s1.split("|");
        for (int i = 0; i < S2.length; i++) {
            System.out.print(S2[i] );
        }
        System.out.println();
        String s2 = "hello world hello bit";
        String[] S3 = s2.split(" ",2);//以空格为间隔将字符串分为两份
        for (int i = 0; i < S3.length; i++) {
            System.out.println(S3[i]);
        }
    }
    public static void main9(String[] args) {
        //也是产生了新的对象
        String s1 = "abcdabcd";
        System.out.println(s1.replace("ab", "ooooo"));//把所有的ab替换成oooo
        System.out.println(s1.replace('a', 'z'));
        System.out.println(s1.replaceFirst("ab", "kk"));//只替换第一个ab
        System.out.println(s1.replaceAll("ab", "kk"));
    }
    public static void main8(String[] args) {
        String s1 = "abcd";
        //把字符串转变为字符数组
        char[] arr = s1.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        //格式化
        System.out.println(String.format("%d-%d-%d", 2024, 11,26));
    }
    public static void main7(String[] args) {
        String s1 = "abcd";
        String S1 = "ABCD";
        //转变成大写
        //并不是在员对象上进行转换而是生成了一个新对象
        System.out.println(s1.toUpperCase());
        //转变成小写
        System.out.println(S1.toLowerCase());
    }
    public static void main6(String[] args) {
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String S3 = String.valueOf('t');
        String s4 = String.valueOf(true);
        String s5 = String.valueOf(new Student());
        System.out.println(s5);
    }
    public static void main5(String[] args) {
        String s1 = "abcdefg";
        //获取指定位置的字符
        System.out.println(s1.charAt(2));//输出字符串s1下标为二的值
        //获取指定字符第一次出现的位置
        System.out.println(s1.indexOf('c'));
        System.out.println(s1.indexOf("cd"));//这里返回c的位置
        System.out.println(s1.indexOf("c", 3, 5));//开始查找的位置，结束查找的位置，找不到返回-1
        String s2 = "abcdcdaacde";
        System.out.println(s2.lastIndexOf('c'));//从后往前找c
        System.out.println(s2.lastIndexOf('c', 7));
    }
    public static void main4(String[] args) {
        String s1 = "ABCD";
        String s2 = "abcd";
        System.out.println(s1.compareToIgnoreCase(s2));             //忽略大小写比较字符串大小
    }
    public static void main3(String[] args) {
        String s1 = "abcd";
        String s2 = "acbd";
        //1、如果字符串长度相同，比较的是第一个不同元素的大小  c-b = 1
        //2、如果长度不同，那么先比较两个长度的差值，在这个差值范围内，有不一样的字符就能比较出来大小，如果不一样，就看谁长谁大
        //s2大于s1返回正数
        //   等于      0
        //   小于      负数
        System.out.println(s2.compareTo(s1));
        String s4 = "abcdefg";
        String s3 = "abcd";
        String s5 = "acbd";
        System.out.println(s4.compareTo(s5));   //-1
        System.out.println(s4.compareTo(s3));   //3     len s4 - len s3
    }
    public static void main2(String[] args) {
        String str1 = new String("abcdefg");
        String str = new String("abcdefg");
        System.out.println(str == str1);//此时比较的是地址
        System.out.println(str.equals(str1));    //此时比较字符串的内容
        //**********************************************
        String str3 = "abcdef";
        String str4 = "abcdef";
        System.out.println(str3==str4);//字符串常量池   需要哈希表
    }
    public static void main1(String[] args) {
        String str = "abcdef";
        System.out.println(str.length());               //String类计算长度的方法
        System.out.println("6666".length());
        System.out.println(str.isEmpty());              //判断str是否为空的方法
        String str1 = new String("abcdefg");    //区别于C语言，Java的字符串没有以\0结尾的说法
        char[] chars = {'a','b','c'};
        String s = new String((chars));
        System.out.println(s);
        //**************************************************
        String str4 = "";
        System.out.println(str4.isEmpty());         //true
        String str3 = null;     //str3这个引用引用指向了空对象
        System.out.println(str3.isEmpty());         //空指针异常
    }
}
